package com.his.controller;


import com.his.commons.core.dto.Result;
import com.his.commons.core.exception.HisException;
import com.his.commons.core.util.TimeUtil;
import com.his.commons.web.exception.ParamException;
import com.his.commons.web.util.ValidatedUtil;
import com.his.controller.form.*;
import com.his.controller.info.ScheduleInfo;
import com.his.controller.info.UserByDayInfo;
import com.his.service.ScheduleService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 梅津铧
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/schedule")
@Api(tags = "排班接口")
@GlobalTransactional(rollbackFor = Exception.class)
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    /**
     * 根据科室、日期信息获取医生信息
     * @param DepartmentId  科室id
     * @param DisagnosisRoomId  诊室id
     * @param date  日期
     * @param time  午别
     * @return  医生信息
     * @throws HisException
     */
    @ApiOperation("根据科室、日期信息获取医生信息")
    @GetMapping("/obtainUserByDay")
    public Result<List<UserByDayInfo>> ObtainUserByDay(
            @RequestParam(value = "DepartmentId",required = false) Integer DepartmentId,
            @RequestParam(value = "DisagnosisRoomId",required = false) Integer DisagnosisRoomId,
            @RequestParam(value = "date",required = false)String date,
            @RequestParam(value = "time",required = false)String time
    ) throws HisException{
        ScheduleByDayForm scheduleByDayForm = new ScheduleByDayForm();
        scheduleByDayForm.setDepartmentId(DepartmentId);
        scheduleByDayForm.setDisagnosisRoomId(DisagnosisRoomId);
        scheduleByDayForm.setDate(date);
        scheduleByDayForm.setTime(time);

        List<UserByDayInfo> userByDayInfos = scheduleService.obtainByDay(scheduleByDayForm);
        if(null == userByDayInfos | userByDayInfos.size()==0) throw new HisException("获取医生信息失败",500);
        return Result.getInstanceList(UserByDayInfo.class).setData(userByDayInfos);
    }

    /**
     * 创建某诊室的医生排班信息
     * @param newScheduleForm  排班信息
     * @param bindingResult
     */
    @ApiOperation("创建某诊室的医生排班信息")
    @PostMapping("/increaseSchedules")
    public Result IncreaseSchedules(@RequestBody @Valid NewScheduleForm newScheduleForm,BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = scheduleService.increaseSchedules(newScheduleForm);
        if(!b) throw new HisException("创建诊室"+newScheduleForm.getScheduleUserName()+"的排班信息失败",500);
        return Result.getInstance().setMsg("成功创建诊室排班信息");
    }

    /**
     * 清空某诊室医生的排班信息
     * @param form  诊室
     * @param bindingResult
     */
    @ApiOperation("清空某诊室医生的排班信息")
    @PutMapping("/moditySchedulesState")
    public Result ModifySchedulesState(@RequestBody @Valid ScheduleClearForm form,BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = scheduleService.modifyState(form);
        if(!b) throw new HisException("清空某诊室医生的排班信息失败",500);
        return Result.getInstance();
    }

    /**
     * 注册某诊室医生的排班信息
     * @param form  某诊室的排班信息
     * @param bindingResult
     */
    @ApiOperation("注册某诊室医生的排班信息")
    @PutMapping("/moditySchedulesByInfo")
    public Result ModifySchedulesByInfo(@RequestBody @Valid ScheduleInfoForm form, BindingResult bindingResult) throws HisException {
        ValidatedUtil.ValidatedException(bindingResult);

        boolean b = scheduleService.modifyByInfo(form);
        if(!b) throw new HisException("注册排班信息失败",500);
        return Result.getInstance();
    }

    /**
     * 根据科室、诊室查询排班信息
     * @param DepartmentId  科室id
     * @param DisagnosisRoomId  诊室id
     * @return  排班信息
     * @throws ParamException
     */
    @ApiOperation("根据科室、诊室查询排班信息")
    @GetMapping("/obtainSchedule")
    public Result<List<ScheduleInfo>> ObtainSchedule(@RequestParam(value = "DepartmentId",required = false) Integer DepartmentId,
                                                     @RequestParam(value = "DisagnosisRoomId",required = false) Integer DisagnosisRoomId) throws HisException {

        List<ScheduleInfo> scheduleInfos = scheduleService.obtainSchedule(DepartmentId,DisagnosisRoomId);
        if(null == scheduleInfos | scheduleInfos.size()==0) throw new HisException("未查询到排班信息",500);
        return Result.getInstanceList(ScheduleInfo.class).setData(scheduleInfos);
    }

    /**
     * 清除某科室的排班信息
     * @param scheduleUserIds  员工工号
     * @param departmentId  科室id
     * @param disagnosisId  诊室id
     * @throws HisException
     */
    @ApiOperation("清除某科室的排班信息")
    @DeleteMapping("/logoutSomeUser")
    public Result LogoutSchedule(@RequestParam(value = "scheduleUserIds",required = false) List<Integer> scheduleUserIds,
        @RequestParam(value = "departmentId",required = false) Integer departmentId,
        @RequestParam(value = "disagnosisId",required = false) Integer disagnosisId
    ) throws HisException {

        boolean b = scheduleService.logoutSchedule(scheduleUserIds,departmentId,disagnosisId);
        if(!b) throw new HisException("清除排班信息失败",500);
        return Result.getInstance();
    }

}

